package com.whfc.ms.api.fse.controller;

import com.whfc.common.base.BaseController;
import com.whfc.common.result.ListData;
import com.whfc.common.result.PageData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.fse.dto.FseCraneDTO;
import com.whfc.fse.dto.FseCraneFvsDeviceDTO;
import com.whfc.fse.dto.FseCraneToolDTO;
import com.whfc.fse.param.*;
import com.whfc.fuum.entity.SysUser;
import com.whfc.ms.api.fse.service.MsFseCraneService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @DESCRIPTION
 * @AUTHOR GuoDong_Sun
 * @DATE 2020/4/8
 */
@RequiresPermissions("fse/crane")
@RestController
@RequestMapping("/ms/api/crane")
public class FseCraneController extends BaseController {
    @Autowired
    private MsFseCraneService msFseCraneService;

    @GetMapping(value = "/list", params = {"pageNum", "pageSize"})
    public Result list(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestParam("pageNum") Integer pageNum,
                       @RequestParam("pageSize") Integer pageSize,
                       @RequestParam("deptId") Integer deptId,
                       @RequestParam(value = "keyword", required = false) String keyword,
                       @RequestParam(value = "bindFlag", required = false) Integer bindFlag,
                       @RequestParam(value = "netState", required = false) Integer netState) {
        logger.info("塔机列表,loginer:{},pageNum:{},pageSize:{},deptId:{},keyword:{}",
                user.getUsername(), pageNum, pageSize, deptId, keyword);
        PageData<FseCraneDTO> pageData = msFseCraneService.list(pageNum, pageSize, deptId, keyword, bindFlag, netState);
        return ResultUtil.success(pageData);
    }

    @GetMapping("/list")
    public Result list(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestParam("deptId") Integer deptId,
                       @RequestParam(value = "bindFlag", required = false) Integer bindFlag) {
        logger.info("塔机列表,loginer:{},deptId:{},bindFlag:{}", user.getUsername(), deptId, bindFlag);
        ListData<FseCraneDTO> listData = msFseCraneService.list(deptId, bindFlag);
        return ResultUtil.success(listData);
    }

    @GetMapping("/detail")
    public Result detail(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                         @RequestParam("craneId") Integer craneId) {
        logger.info("塔机详情,loginer:{},craneId:{}", user.getUsername(), craneId);
        FseCraneDTO fseCraneDTO = msFseCraneService.detail(craneId);
        return ResultUtil.success(fseCraneDTO);
    }

    @RequiresPermissions("fse/crane/device/add")
    @PostMapping("/add")
    public Result add(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                      @RequestBody @Validated FseCraneAddParam request) {
        logger.info("添加塔机,loginer:{},param:{}", user.getUsername(), request.toString());
        msFseCraneService.add(request);
        return ResultUtil.success();
    }

    @RequiresPermissions("fse/crane/device/edit")
    @PostMapping("/edit")
    public Result edit(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestBody @Validated FseCraneEditParam request) {
        logger.info("编辑塔机,loginer:{},param:{}", user.getUsername(), request.toString());
        msFseCraneService.edit(request);
        return ResultUtil.success();
    }

    @RequiresPermissions("fse/crane/device/del")
    @PostMapping("/del/{id}")
    public Result del(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                      @PathVariable("id") Integer id) {
        logger.info("删除塔机,loginer:{},id:{}", user.getUsername(), id);
        msFseCraneService.del(id);
        return ResultUtil.success();
    }

    @PostMapping("/bind")
    public Result bind(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestBody @Validated FseCraneBindParam request) {
        logger.info("塔机绑定硬件,loginer:{},param:{}", user.getUsername(), request.toString());
        msFseCraneService.bind(request);
        return ResultUtil.success();
    }

    @PostMapping("/unbind/{craneId}")
    public Result unbind(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                         @PathVariable("craneId") Integer craneId) {
        logger.info("塔机解绑硬件，loginer:{},craneId:{}", user.getUsername(), craneId);
        msFseCraneService.unbind(craneId);
        return ResultUtil.success();
    }

    @PostMapping("/bind/fvs")
    public Result bindFvs(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                          @RequestBody @Validated FseCraneFvsParam request) {
        logger.info("塔机绑定监控设备，loginer:{},request:{}", user.getUsername(), request.toString());
        msFseCraneService.bindFvs(request);
        return ResultUtil.success();
    }

    @GetMapping("/fvs/list")
    public Result fvsList(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                          @RequestParam("craneId") Integer craneId) {
        logger.info("获取视频监控列表，loginer:{},craneId:{}", user.getUsername(), craneId);
        List<FseCraneFvsDeviceDTO> fvsList = msFseCraneService.fvsList(craneId);
        return ResultUtil.success(new ListData<>(fvsList));
    }


    @GetMapping("/tool/list")
    public Result craneToolList(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                @RequestParam("deptId") Integer deptId,
                                @RequestParam("pageNum") Integer pageNum,
                                @RequestParam("pageSize") Integer pageSize) {
        logger.info("获取吊索具列表，loginer:{},deptId:{},pageNum:{},pageSize:{}", user.getUsername(), deptId, pageNum, pageSize);
        PageData<FseCraneToolDTO> data = msFseCraneService.getCraneToolList(deptId, pageNum, pageSize);
        return ResultUtil.success(data);
    }

    @RequiresPermissions("fse/crane/rigging/add")
    @PostMapping("/tool/add")
    public Result craneToolAdd(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                               @RequestBody @Validated FseCraneToolAdd param) {
        logger.info("新增吊索具信息，loginer:{},param:{}", user.getUsername(), param);
        msFseCraneService.addCraneTool(param);
        return ResultUtil.success();
    }

    @RequiresPermissions("fse/crane/rigging/edit")
    @PostMapping("/tool/edit")
    public Result craneToolEdit(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                @RequestBody @Validated FseCraneToolEdit param) {
        logger.info("编辑吊索具信息，loginer:{},param:{}", user.getUsername(), param);
        msFseCraneService.editCraneTool(param);
        return ResultUtil.success();
    }

    @RequiresPermissions("fse/crane/rigging/delete")
    @PostMapping("/tool/del/{toolId}")
    public Result craneToolEdit(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                @PathVariable("toolId") Integer toolId) {
        logger.info("删除吊索具信息，loginer:{},toolId:{}", user.getUsername(), toolId);
        msFseCraneService.delCraneTool(toolId);
        return ResultUtil.success();
    }
}
